/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package crypbox.gui;

import crypbox.net.backend.Backend;
import java.awt.AWTException;
import java.awt.CheckboxMenuItem;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author MJ
 */
public class Tray {

    private static TrayIcon trayIcon;
    public static  void registerTray(){


         if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
             
            Image image = Toolkit.getDefaultToolkit().getImage("src/res/icon.png");


            MouseListener mouseListener = new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
//                        if (frame != null) {
//                            frame.dispose();
//                        }
//                        frame = new ConfigFrame();
                        java.awt.EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
//                                frame.setVisible(true);
//                                frame.requestFocus();
                            }
                        });
                    }
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    if(Backend.getInstance().getStatus().length() > 0){
                    trayIcon.setToolTip("Cryptbox - " + Backend.getInstance().getStatus());
                    }else{
                        trayIcon.setToolTip("Cryptbox");
                    }
                }
            };

            ActionListener exitListener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    
                    System.exit(0);
                }
            };

            PopupMenu popup = new PopupMenu();
            CheckboxMenuItem pruefeOrdner = new CheckboxMenuItem("Überwache ordner", true);

            ItemListener checkboxListener = new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent e) {

                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        Backend.getInstance().startWatching();
                    } else {
                        Backend.getInstance().stopWatching();
                    }
                }
            };
            pruefeOrdner.addItemListener(checkboxListener);

            MenuItem defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);
            popup.add(pruefeOrdner);
            popup.add(defaultItem);


            trayIcon = new TrayIcon(image, "Cryptbox", popup);

//            ActionListener actionListener = new ActionListener() {
//
//                public void actionPerformed(ActionEvent e) {
//                    trayIcon.displayMessage("Action Event",
//                            "An Action Event Has Been Performed!",
//                            TrayIcon.MessageType.INFO);
//
//                }
//            };


            trayIcon.setImageAutoSize(true);
            trayIcon.addMouseListener(mouseListener);


            int i = 0;
            while (i != -1 && i < 4) {
                try {
                    tray.add(trayIcon);
                    i = -1;
                } catch (AWTException e) {
                    System.err.println("TrayIcon could not be added.");
                    i++;
                }
            }

        } else {
            //  System Tray is not supported
        }




    }
    

}
